/*********** JS*********/

//$(document).ready(function (){
$(function(){
    animate();

    // Close Tab Check out (Popup shopping bag) and Hide background when mouse leave
    var x;
    $('#ppShoppingbag').mouseleave(function() {
        x = setTimeout("$('.close-reveal-modal').click()", 4000);
    });
    $('#ppShoppingbag').mouseenter(function() {
        clearTimeout(x);
    });
});

/*********** BANNER *********/
function animate() {
    function fadeTo(lis, duration, opacity, callback) {
        if (!lis.length) {
            callback();
        }

        var $lis = $(lis.shift());
        $lis.delay(700).fadeTo(duration, opacity, function () {
            fadeTo(lis, duration, opacity, callback);
        })
    }

    fadeTo($('#fds li').get(), 700, 1, function () {
        setTimeout(function () {
            fadeTo($('#fds li').get(), 0, 0, function () {
                setTimeout(animate, 0);
            })
        }, 5000);
    })
}

function showMessage(msg){
    $('#message-detail').html(msg);

    $('#modal-content').popUpWindow({
        action: "open",
        size: "small"
    });

    $('#modal-content-close').bind('click', function(e){
        e.preventDefault();
        $('#modal-content').popUpWindow({action: 'close'});
    });
}

function addToCart(product_id, quantity) {
    quantity = typeof(quantity) != 'undefined' ? quantity : 1;

    $.ajax({
        url: 'index.php?route=module/cart/add', //'index.php?route=checkout/cart/add',
        type: 'post',
        data: 'product_id=' + product_id + '&quantity=' + quantity,
        dataType: 'json',
        success: function(json) {
            if (json['success']) {
                $('#cartPopupData').html(json['html']);
                $('#cart_mini_product').html(json['product']);
                $('#cart_mini_total').html(json['total']);

                $('#ppShoppingbag').reveal();
            }
        }
    });
}

function removeFromCart(product_key, checkout_mode) {
    $.ajax({
        url: 'index.php?route=module/cart/remove',
        type: 'get',
        data: 'remove=' + product_key + '&checkout_mode=' + checkout_mode,
        dataType: 'json',
        success: function(json) {
            if (json['success']) {
                $('#cartPopupData').html(json['html']);
                $('#cart_mini_product').html(json['product']);
                $('#cart_mini_total').html(json['total']);

                if(json['checkout_mode']){
                    window.location.href = json['checkout_mode'];
                }
            }
        }
    });
}

function updateCart(product_key, quantity_id, increase, checkout_mode) {
    var qty = $('#' + quantity_id).val() * 1;
    var process = false;

    if(increase){
        qty += 1;
        process = true;
    }
    else{
        if(qty > 1){
            qty -= 1;
            process = true;
        }
    }

    if(process){
        $.ajax({
            url: 'index.php?route=module/cart/update',
            type: 'post',
            data: 'quantity[' + product_key + ']=' + qty + '&checkout_mode=' + checkout_mode,
            dataType: 'json',
            success: function(json) {
                if (json['success']) {
                    $('#cartPopupData').html(json['html']);
                    $('#cart_mini_product').html(json['product']);
                    $('#cart_mini_total').html(json['total']);

                    if(json['checkout_mode']){
                        window.location.href = json['checkout_mode'];
                    }
                }
            }
        }).done(function() {
            //$('#' + quantity_id).val(qty);
            var $qtyInput = $('#' + quantity_id);
            $qtyInput.animate({backgroundColor: '#008341'}, 250, function(){
                $qtyInput.removeAttr('style');
            });

        });
    }
}

function isValidEmail(email){
    var mail_pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{1,6})+$/;

    return mail_pattern.test(email);
}